package com.lqian.demo05;

/**
 * 只需要保证count ++ 和print语句 保证在同一个线程执行完成就可以
 * @author：Melody_LQ
 * @time：2022/8/22 7:27 下午
 */
public class AltermatelyOutput1To100_003 extends Thread{

    private static int count = 0;
    private int id ; //线程编号

    public AltermatelyOutput1To100_003(int id){
        this.id = id;
    }

    @Override
    public void run() {
        while (count < 100){
            while (count% 2 == id){
                synchronized (AltermatelyOutput1To100_003.class){
                    count ++;                   
                    System.out.println("thread_"+id+" num--> " + count);
                }

            }
        }
    }

    public static void main(String[] args) {
        new AltermatelyOutput1To100_003(0).start();
        new AltermatelyOutput1To100_003(1).start();
    }
}
